/**
 * 
 * @authors Riche
 * @date    2016-07-06 14:39:22
 * @version 1.1.0
 */
var topicID = $.getUrlVar("id");
var parentID = $.getUrlVar("pid");
var backUrl = $.getUrlVar("back");
if (parentID == undefined) {
    parentID = 0;
}

var vm = new Vue({
    el: "#app",
    data: {
        title: '',
        typeID: '',
        addShow: false,
        addphotoshow: false,
        blocks: [],
        askTitle: '',
        askContent: '',
        contentPicture: []
    },
    compiled: function() {
        var _self = this;
        _self.loadPhoto(); //调用图片预加载

        // Common.ajaxCall({ url: "ask_getTypeList", datatype: 'json', type: 'get'}, function (data) {
        //     if(data.error_code==0){
        //         _self.blocks = data.data;
        //     }else{
        //         alert(data.msg);
        //     }
        // });

        // 选择图片
        document.getElementById('pic').onchange = function (e) {
            event = e || event;
            var img = event.target.files[0];
            // 判断是否图片  
            if (!img) {
                return;
            }
            // 判断图片格式  
            if (!(img.type.indexOf('image') == 0 && img.type && /\.(?:jpg|png|gif)$/.test(img.name))) {
                alert('图片只能是jpg,gif,png');
                return;
            }

            //定义图片参数
            var uploadFile = new FormData();
            uploadFile.append("uploadFile", img);

            //接口方面的更新，现在上传图片必须得有accessToken等
            getaccessToken(function(token) {

                uploadFile.append("folderId", $.getUrlVar("folderId"));
                uploadFile.append("accessToken", token);
                uploadFile.append("tags", decodeURIComponent($.getUrlVar("title")));

                Common.ajaxSubmitPic({
                    url: 'upload_file',
                    data: uploadFile,
                }, function(data) {
                    if (data.error_code == 0) {

                        /*
                        var dom = '<li class="imgBox j_1" ><i class="closeImg" onclick="delImg(this);"></i><img src="' + data.data[0].content + '" id="pic1" class="loadImg"></li>';
                        $('#dom').append(dom);
                        //$('#pic1').attr('src',image);
                        _self.contentPicture.push(data.msg);
                        myJS.imgScale('.j_1', '1', '1');
                        myJS.ImgFull('.loadImg','images/liby.png');
                        */

                        //添加图片到内容中去
                        $('#askContent').htmlarea('tofocus');
                        $('#askContent').htmlarea('image', data.data[0].content);

                        //vm.addReplyPic(image);
                    } else {
                        alert(data.msg);
                    }
                    $('#pic').val('');
                });
            });
        }
    },
    methods: {
        back: function() {
            window.location.href = decodeURIComponent(backUrl);
        },
        publish: function() { //提交提问内容
            var _self = this;
            _self.askContent = $('#askContent').htmlarea('html'); //取内容
            if (!_self.askContent.trim()) { //如果没填写内容
                alert('请填写内容');
                $('#askContent').focus();
                return false;
            }
            var data = {
                topicID: topicID,
                comment: _self.askContent,
                contentPicture: _self.contentPicture.join(',')
            }
            var url = "circle_addComment";
            if (parentID > 0) {
                //回复
                data["parentID"] = parentID;
                //url = "circle_addReplyComment";
            }
            //data = Common.jsonEncode(data);
            Common.ajaxCall({ url: url, datatype: 'json', type: 'get', data: data }, function(data) {
                if (data.error_code == 0) {

                    var pic = "lebel.png";
                    var description = "";
                    if (data.data.length > 0) {
                        var obj = data.data[0];
                        var code = obj.code;
                        pic = code == 1 ? "moneyPig.png" : "lebel.png";
                        description = obj.str;
                    }
                    alert({
                        text: '<img src="images/' + pic + '"/>',
                        description: description,
                        kind: 0
                    });

                    //延迟跳转
                    setTimeout(function() { window.location.href = decodeURIComponent(backUrl); }, 1000);
                } else {
                    alert(data.msg);
                }
            });

        },
        addphoto: function() {
            if (this.addphotoshow) {
                this.addphotoshow = false;
            } else {
                this.addphotoshow = true;
            }
        },
        loadPhoto: function() {
            myJS.imgScale('.j_1', '1', '1');
            myJS.imgScale('.j_1511', '15', '11');
            myJS.ImgFull('.loadImg', 'images/liby.png');
        },
        selectPic: function() {
            //选择图片
            $("#pic").click();
        }
    },
    watch: {

    }
});